package xin.lhuo.we.server.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import lombok.extern.slf4j.Slf4j;
import xin.lhuo.we.server.properties.QiNiuProperties;

/**
 * @author Lhuo
 * @date 2019/5/10 11:43
 */
@Slf4j
public class QiNiuUtil {
    /**
     * 获取上传token，用于照片上传
     * 七牛上传成功之后回调服务端，插入数据
     *
     * @param qiNiuProperties
     * @return
     */
    public static String getUpToken(QiNiuProperties qiNiuProperties) {
        Auth auth = Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());
        StringMap putPolicy = new StringMap();
        putPolicy.put("callbackUrl", qiNiuProperties.getCallbackUrl());
        putPolicy.put("callbackBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"mimeType\":\"$(mimeType)\"," +
                "\"fsize\":$(fsize),\"height\":$(imageInfo.height),\"width\":$(imageInfo.width),\"lastModifiedDate\":$(x:lastModifiedDate)," +
                "\"userId\":$(x:userId),\"albumId\":$(x:albumId)}");
        putPolicy.put("callbackBodyType", qiNiuProperties.getCallbackBodyType());
        long expireSeconds = 3600;
        return auth.uploadToken(qiNiuProperties.getBucket(), null, expireSeconds, putPolicy);
    }


    /**
     * 获取上传token，七牛上传成功后无回调
     * @param qiNiuProperties
     * @return
     */
    public static String getUpTokenNoCallBack(QiNiuProperties qiNiuProperties){
        Auth auth = Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());
        return auth.uploadToken(qiNiuProperties.getBucket());
    }

    /**
     * 获取上传token,用于文档上传
     *
     * @param qiNiuProperties
     * @return
     */
    public static String getUpTokenByDocument(QiNiuProperties qiNiuProperties){
        Auth auth = Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());
        StringMap putPolicy = new StringMap();
        putPolicy.put("callbackUrl", qiNiuProperties.getCallbackUrlByDoc());
        putPolicy.put("callbackBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"mimeType\":\"$(mimeType)\"," +
                "\"fsize\":$(fsize),\"userId\":$(x:userId),\"lastModifiedDate\":$(x:lastModifiedDate),\"folderId\":$(x:folderId)}");
        putPolicy.put("callbackBodyType", qiNiuProperties.getCallbackBodyType());
        long expireSeconds = 3600;
        return auth.uploadToken(qiNiuProperties.getBucket(), null, expireSeconds, putPolicy);
    }

    /**
     * 检查上传相册成功后回调是否合法
     * @param qiNiuProperties
     * @param authorization
     * @param callbackBody
     * @return
     */
    public static boolean isValidCallback(QiNiuProperties qiNiuProperties, String authorization, byte[] callbackBody) {
        Auth auth = Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());
        return auth.isValidCallback(authorization, qiNiuProperties.getCallbackUrl(), callbackBody, qiNiuProperties.getCallbackBodyType());
    }

    /**
     * 检查上传文档成功后回调是否合法
     * @param qiNiuProperties
     * @param authorization
     * @param callbackBody
     * @return
     */
    public static boolean isValidDocCallback(QiNiuProperties qiNiuProperties, String authorization, byte[] callbackBody) {
        Auth auth = Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());
        return auth.isValidCallback(authorization, qiNiuProperties.getCallbackUrlByDoc(), callbackBody, qiNiuProperties.getCallbackBodyType());
    }
    /**
     * 获取下载链接token
     * @param qiNiuProperties
     * @param url
     * @return
     */
    public static String getDownloadUrl(QiNiuProperties qiNiuProperties,String url){
        Auth auth = Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());
        return auth.privateDownloadUrl(url);
    }

    public static String getDownloadUrlByKey(QiNiuProperties qiNiuProperties,String key){
        Auth auth = Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());
        String url  = qiNiuProperties.getBaseUrl()+ key;
        return auth.privateDownloadUrl(url);
    }

    /**
     * 删除文件
     * @param qiNiuProperties
     * @param key
     * @throws QiniuException
     */
    public static void deleteFile(QiNiuProperties qiNiuProperties,String key) throws QiniuException {
        Configuration cfg = new Configuration(Zone.zone1());
        Auth auth = Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());
        BucketManager bucketManager = new BucketManager(auth, cfg);
        bucketManager.delete(qiNiuProperties.getBucket(),key);

    }
}
